﻿using AnesSystem.Controls;
using AnesSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AnesSystem.ViewModels
{
    public class RightContentCtrlViewModel : ViewModel
    {
        #region Variables

        private FrameworkElement _currentRightContent;

        private List<FrameworkElement> _controls;

        private FrameworkElement _tableContent;

        private StackPanel _defaultContent;
        private TextBlock layer;
        private static RightContentCtrlViewModel _instance;

        #endregion

        #region Constructor

        public RightContentCtrlViewModel()
        {
            _controls = new List<FrameworkElement>();
            Initial();
        }

        private void _tableContent_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {

        }

        private void _tableContent_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        #endregion

        #region Properties


        public static RightContentCtrlViewModel Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new RightContentCtrlViewModel();
                }
                return _instance;
            }
        }

        public Visibility LayerVisibility
        {
            set
            {
                layer.Visibility = value;
            }
        }


        public FrameworkElement CurrentRightContent
        {
            get
            {
                return _currentRightContent;
            }
            set
            {
                if (value == null)
                {
                    TableContent = _defaultContent;
                    return;
                }
                _currentRightContent = null;
                TableContent = null;
                _currentRightContent = value;
                Type type = _currentRightContent.GetType();
                if (_currentRightContent is BaseTableControl)
                {
                    CreateBaseTableControl();
                }
                else
                {
                    TableContent = _currentRightContent;
                }
                if (_currentRightContent is PrintTemplate)
                    PrintTemplate.PrintVisul = (PrintTemplate)_currentRightContent;
                else
                {
                    PrintTemplate.PrintVisul = null;
                }
                TableContent.HorizontalAlignment = HorizontalAlignment.Center;
            }
        }

        private void CreateBaseTableControl()
        {
            ScrollViewer sv = new ScrollViewer();
            sv.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            if (_currentRightContent.Parent != null)
                (_currentRightContent.Parent as Grid).Children.Clear();
            Grid rootGrid = new Grid();
            rootGrid.Children.Add(_currentRightContent);

            sv.Content = rootGrid;
            layer = new TextBlock();
            layer.Background = System.Windows.Media.Brushes.Transparent;
            layer.Visibility = Visibility.Collapsed;
            layer.Width = CurrentRightContent.Width;
            layer.Height = CurrentRightContent.Height;
            rootGrid.Children.Add(layer);
            TableContent = sv;
        }

        public FrameworkElement TableContent
        {
            get
            {
                return _tableContent;
            }
            set
            {
                _tableContent = value;
                RaisePropertyChanged("TableContent");
            }
        }

        public List<FrameworkElement> Controls
        {
            get
            {
                return _controls;
            }
            set
            {
                _controls = value;
                RaisePropertyChanged("Controls");
            }
        }

        //

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void Initial()
        {
            StackPanel sp = new StackPanel();
            sp.HorizontalAlignment = HorizontalAlignment.Center;
            sp.VerticalAlignment = VerticalAlignment.Center;
            TextBlock lb = new TextBlock();
            lb.Text = "请选择病人";
            lb.FontSize = 50;
            lb.HorizontalAlignment = HorizontalAlignment.Center;
            lb.VerticalAlignment = VerticalAlignment.Center;
            sp.Children.Add(lb);
            _defaultContent = sp;
        }

        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        #endregion
    }
}
